﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Train_IssuePractice : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitializeComponent();
            Master.DataSource = WMaster;
            Detail.DataSource = WMaster;

            string pindex = this.Request.Params["pIndex"];
            if (pindex != null)
            {
                this.wfvMaster.PageIndex = Convert.ToInt32(pindex);
                Detail.ExecuteSelect(wfvMaster);
                DataBind();
            }

            string issid = this.Request.QueryString["issid"];
            if (issid != null)
            {
                //if (issid == "-1")
                //{
                //    WMaster.SetWhere("1=0");
                //}
                //else
                //{
                    int index = 0;
                    DataTable dt = this.Master.InnerDataSet.Tables["Train_Issue"];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["IssueId"].ToString() == issid)
                        {
                            index = i;
                            break;
                        }
                    }
                    this.wfvMaster.PageIndex = Convert.ToInt32(index);
                    Detail.ExecuteSelect(wfvMaster);
                    DataBind();
                //}
            }


            MasterPage_MasterPage mpg = (MasterPage_MasterPage)this.Page.Master;
            mpg.HelpHref = "~/Help/Train/培训实施.htm";
            mpg.PageName = "主题实施";
            mpg.PicUrl = "";
            mpg.IntiBar();
        }
    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Train_IssuePractice));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        //  
        this.WMaster.Active = true;
        this.WMaster.AlwaysClose = false;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "Train.Train_Issue";
        this.WMaster.ServerModify = true;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }

    protected void WebNavigator1_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "cmdFirst" || e.CommandName == "cmdPrevious"
            || e.CommandName == "cmdNext" || e.CommandName == "cmdLast")
        {
            Detail.ExecuteSelect(wfvMaster);
            DataBind();
        }
        else if (e.CommandName == "cmdAdd")
        {
            Detail.ExecuteAdd(wfvMaster);
            DataBind();
        }
        else if (e.CommandName == "cmdDelete")
        {
            Detail.ExecuteSelect(wfvMaster);
            DataBind();
        }
        else if (e.CommandName == "cmdApply" && wfvMaster.AllValidateSucess)
        {
            Detail.ExecuteSelect(wfvMaster);
            DataBind();
        }

    }

    protected void wfvMaster_PageIndexChanged(object sender, EventArgs e)
    {
        Detail.ExecuteSelect(wfvMaster);
        DataBind();
    }

    protected void wfvMaster_AfterInsertLocate(object sender, EventArgs e)
    {
        Detail.ExecuteSelect(wfvMaster);
        DataBind();
    }

    protected void wfvMaster_Canceled(object sender, EventArgs e)
    {
        Detail.ExecuteSelect(wfvMaster);
        DataBind();
    }

    protected void btnAdd_Click(object sender, ImageClickEventArgs e)
    {
        TextBox tbIssId = (TextBox)this.wfvMaster.FindControl("IssueIdTextBox");
        string IssId = tbIssId.Text;
        int pIndex = this.wfvMaster.PageIndex;
        string js;
        if (rbDisp.SelectedValue == "列表选择")
        {
            js = "window.open('SelEmployees.aspx?IssId={0}&pIndex={1}','','height=700,width=600,status=no,toolbar=no,menubar=no,location=no,resizable=no')";
        }
        else 
        {
            js = "window.open('SelEmployees2.aspx?IssId={0}&pIndex={1}','','height=700,width=600,status=no,toolbar=no,menubar=no,location=no,resizable=no')";
        }
  
        js = string.Format(js, IssId, pIndex);
        this.ClientScript.RegisterStartupScript(this.GetType(), "", js, true);
    }

    protected void Calc_Click(object sender, EventArgs e)
    {
        string SqlSelect = "select count(*) from Train_Practice where IssueId=" + (sender as Button).CommandArgument.ToString();
        DataTable dtt = Srvtools.CliUtils.ExecuteSql("GLModule", "cmdRefValUse",SqlSelect,true, Srvtools.CliUtils.fCurrentProject).Tables[0];
        if (dtt != null && dtt.Rows.Count > 0)
        {
            int Num = Convert.ToInt32(dtt.Rows[0][0]);
            if (Num != 0)
            {
                double money = Master.CurrentRow["TrainICharge"].ToString() != "" ? Convert.ToDouble(Master.CurrentRow["TrainICharge"]) : 0;
                string UP = "update Train_Practice set Charge=" + money / Num + " where IssueId=" + (sender as Button).CommandArgument.ToString();
                Srvtools.CliUtils.ExecuteSql("GLModule", "cmdRefValUse", UP, true, Srvtools.CliUtils.fCurrentProject);
                ClientScript.RegisterStartupScript(GetType(), "", "alert('更新成功！');location.href='IssuePractice.aspx?issid=" + Master.CurrentRow["IssueId"].ToString() + "'", true);
            }
        }
    }
}
